<template>
  <view>
    <pageModule
      :pageTitle="type ? '修改密码' : '忘记密码'"
      @submit="submit"
      :disabled="getDisabled"
      :btnText="type ? '确认' : '下一步'"
    >
      <template #header>
        <template v-if="!type">
          <pageInp
            label="手机号"
            placeholder="请输入手机号"
            inputType="tel"
            :value.sync="form.phone"
          ></pageInp>
          <pageInp
            label="验证码"
            isGetCode
            placeholder="请输入验证码"
            :value.sync="form.verify_code"
            code_type="forget_password"
            :phone="form.phone"
          ></pageInp>
        </template>
        <template v-else>
          <pageInp
            label="账户密码"
            placeholder="请输入密码"
            inputType="password"
            :value.sync="form.password"
          ></pageInp>
          <pageInp
            label="确认密码"
            placeholder="请确认密码"
            inputType="password"
            :value.sync="form.re_password"
          ></pageInp>
        </template>
      </template>
    </pageModule>
  </view>
</template>

<script>
import pageModule from "../components/page-module";
import pageInp from "../components/page-inp";
import pagePrivacy from "../components/page-privacy.vue";
import { regMobile } from "@/common/regExp";

export default {
  components: {
    pageModule,
    pageInp,
    pagePrivacy
  },

  computed: {
    getDisabled() {
      const { form, type } = this;
      if (type) {
        if (form.password) return false;

        return true;
      } else {
        if (form.phone) return false;

        return true;
      }
    }
  },

  data() {
    return {
      type: 0,
      form: {
        phone: "",
        verify_code: "",
        password: "",
        re_password: ""
      }
    };
  },
  methods: {
    async submit() {
      let { form, type } = this;
      if (!type) {
        if (!form.phone) return this.$u.toast("请输入手机号");
        if (!regMobile(form.phone))
          return this.$utils.toast("请填写正确的手机号~");
        if (!form.verify_code) return this.$u.toast("请输入验证码");

        this.type = 1;
        return;
      } else {
        if (!form.password) return this.$u.toast("请输入密码");
        if (!form.re_password) return this.$u.toast("请输入确认密码");
        if (form.password !== form.re_password)
          return this.$u.toast("两次密码输入不一致");
      }

      try {
        const res = await this.$allrequest.user.forgetPassword(form, true);

        if (!res.code) {
          this.$utils.showModal("提示", "修改成功", false, () => {
            this.$utils.goBack();
          });
        }
      } catch (error) {
        this.$u.toast(error);
      }
    }
  }
};
</script>
